require 'flex_view/components'

class FlexView::ComponentResolver
  
  # Takes a dom node and 'converts' it into
  # a FlexView component
  def self.resolveComponent(view, domNode)  
    # Check if it's a real component
    # or just a primitive node
    if domNode.kind_of? REXML::Element
      if domNode.namespace.empty?
        return FlexView::Components::Primitive.new(view, domNode)
      else
        if FlexView::FlexView.config[:components][domNode.prefix + ':' + domNode.name]
          className = FlexView::FlexView.config[:components][domNode.prefix + ':' + domNode.name][:class]
          return eval(className).new(self, domNode)
        else
          raise FlexView::ComponentNotFoundException, "The component [#{domNode.prefix}#{domNode.name}] could not be found."
        end
      end
    elsif domNode.kind_of? REXML::Text
      return FlexView::Components::TextNode.new(self, domNode)
    end
  end
end